package com.itxiaowu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itxiaowu.dao.CheckGroupDao;
import com.itxiaowu.dao.SetmealDao;
import com.itxiaowu.entity.PageResult;
import com.itxiaowu.entity.QueryPageBean;
import com.itxiaowu.pojo.CheckGroup;
import com.itxiaowu.pojo.Setmeal;
import com.itxiaowu.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    /**
     * 添加套餐
     *
     * @param checkgroupIds
     * @param setmeal
     */
    @Override
    public void addSetmeal(Integer[] checkgroupIds, Setmeal setmeal) {

        //添加套餐
        setmealDao.addSetmeal(setmeal);
        System.out.println("主键值：" + setmeal.getId());
        //添加套餐关联的检查组
        setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);

    }

    /**
     * 进行分页查询
     *
     * @param queryPageBean
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealDao.findPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 查询全部套餐信息
     */
    @Override
    public List<Setmeal> getSetmeal() {

        return setmealDao.getSetmeal();

    }

    /**
     * 根据套餐id值查询关联套餐的数据
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {

        return setmealDao.findById(id);

    }

    /**
     * 根据套餐id 查询关联 检查组的全部id
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> getcheckgroupIdsBySetmealId(Integer id) {

        return checkGroupDao.getcheckgroupIdsBySetmealId(id);

    }

    /**
     * 编辑: 重新修改关系
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void editSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {

        setmealDao.editSetmeal(setmeal);
        setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);

    }

    //设置检查组和套餐的关系
    public void setSetmealAndCheckGroup(Integer setmealId,Integer[] checkgroupIds){

        if(checkgroupIds == null){
            return;
        }
        for (Integer checkgroupId : checkgroupIds) {
            Map<String,Integer> map = new HashMap<>();

            map.put("setmeal_id",setmealId);
            map.put("checkgroup_id",checkgroupId);

            setmealDao.addSetmealAndCheckGroup(map);
        }

    }
    /**
     * 根据id 删除套餐信息
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        // 删除中间表信息
        setmealDao.deleteRelationById(id);
        // 删除套餐信息
        setmealDao.delete(id);

    }
}
